﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AllInOne.FeeUtils;
using System.Threading;

namespace AllInOne.Pages
{
    /// <summary>
    /// FeePage.xaml 的交互逻辑
    /// </summary>
    public partial class FeePage : Page
    {
        public FeePage()
        {
            InitializeComponent();
        }

        private MainWindow parent;

        public FeePage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        public void checkPayState() {
            if (this.jym!=null)
            {
                this.feeUtil.CheckPayState(this.jym, delegate (bool payState)
                {
                    if (payState)
                    {
                        this.jym = null;
                        this.parent.ShowVerifyCode(this.parent.businessInfo.lsh);
                    }
                    else {
                        this.checkPayState();
                    }
                });
            }
        }



        private FeeUtil feeUtil = new FeeUtil();
        private string jym = null;

        public void InitPage(string lsh)
        {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            feeUtil.Login(delegate (bool loginResult)
            {
                if (loginResult)
                {
                    feeUtil.GetQrCodeUrl(lsh, delegate (string qrCodeUrl) {
                        jym = qrCodeUrl.Substring(qrCodeUrl.LastIndexOf("/") + 1, 19);
                        this.Dispatcher.Invoke(new Action(delegate ()
                        {
                            string jpgUrl = FeeUtil.FEE_URL + "/" + qrCodeUrl;
                            SimpleConsole.WriteInfo(string.Format("qrCodeUrl:{0}",jpgUrl), this);
                            this.imgQrCode.Source = new BitmapImage(new Uri(FeeUtil.FEE_URL+"/"+qrCodeUrl));

                        }));
                    });

                }
            });
                
        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }

        private void btnCash_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.PrintTicket();
        }

        private void btnDone_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ShowVerifyCode(this.parent.businessInfo.lsh);

        }
    }
}
